package com.huxin.servelt.admin;

import com.huxin.pojo.Bookreservation;
import com.huxin.pojo.BooksInfo;
import com.huxin.pojo.UserInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/cancel")
public class CancelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求方法
        req.setCharacterEncoding("utf-8");
        //处理响应乱码
        resp.setContentType("text/json;charset=utf-8");
        //获取id值
        Integer bid = Integer.valueOf(req.getParameter("bid"));
        Userservice service = new Userservice();

        List<Bookreservation> bookreservations=service.selectAllreservation();
        Bookreservation bean=null;
        //查找bookreservation表中对应的信息
        for (Bookreservation b : bookreservations) {
            if (bid==b.getBid()){
                bean=b;
            }
        }
        //从预约表中删除数据
        int i=service.delectereservation(bid);
        //拿到预约人的id
        int aid=bean.getAid();
        //通过bid在查找书的信息
        BooksInfo booksInfo=service.selectBookById(bid);
        //查询用户的信息
        UserInfo userInfo = service.selectById(aid);
        //取消预约 书籍数目+1
        int j=service.cancelbooks(bid);
        if(i>0&&j>0){
            resp.getWriter().write("1");//取消成功
        }
        else{
            resp.getWriter().write("0");//取消失败
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
